package com.genghis.rapidcrm.web.view;

import com.genghis.rapidcrm.web.view.language.RapidLanguageHandler;
import com.genghis.rapidcrm.web.view.mapper.RapidRequestModuleMapping;
import com.genghis.rapidcrm.web.view.mapper.RapidRequestModuleMappingHandler;
import com.genghis.rapidcrm.web.view.support.RapidThemeHandler;
import edu.jetwinner.common.EasyStringUtil;
import freemarker.template.SimpleHash;
import org.springframework.beans.BeansException;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.theme.FixedThemeResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Map;

/**
 * @author xulixin
 */
public class RapidFreeMarkerView extends FreeMarkerView {

    protected RapidRequestModuleMappingHandler moduleMappingHandler = new RapidRequestModuleMappingHandler();

    protected RapidRequestModuleMapping requestModuleMapping = new RapidRequestModuleMapping();

    protected ThemeResolver themeResolver;

    @Override
    protected void initApplicationContext() throws BeansException {
        this.themeResolver = getAppContextBean(ThemeResolver.class);
        if (this.themeResolver == null) {
            FixedThemeResolver fixedThemeResolver = new FixedThemeResolver();
            fixedThemeResolver.setDefaultThemeName(getAppConfig().getProperty("rapidcrm.skin.default", "alphagrey"));
            this.themeResolver = fixedThemeResolver;
        }
    }

    protected PropertyResolver getAppConfig() {
        if (getApplicationContext() != null) {
            return getApplicationContext().getEnvironment();
        }
        return new StandardEnvironment();
    }

    protected <T> T getAppContextBean(Class<T> requiredType) {
        try {
            return getApplicationContext().getBean(requiredType);
        } catch (Exception e) {
            return null;
        }
    }

    protected String getModule(HttpServletRequest request, boolean raw) {
        if (!this.requestModuleMapping.getHasMapping()) {
            this.moduleMappingHandler.handleMatch(request, this.requestModuleMapping);
            this.requestModuleMapping.setHasMapping(true);
        }
        String moduleName = requestModuleMapping.getModule();
        if(!raw) {
            String parentModule = requestModuleMapping.getParent();
            if(EasyStringUtil.isNotBlank(parentModule)) {
                moduleName = parentModule + ":" + moduleName;
            }
        }
        return moduleName;
    }

    protected RapidThemeHandler getThemeHandler(HttpServletRequest request) {
        RapidThemeHandler themeHandler = (RapidThemeHandler) request.getAttribute(RapidThemeHandler.RAPID_THEME_HANDLER_KEY);
        if (themeHandler == null) {
            themeHandler = new RapidThemeHandler(themeResolver.resolveThemeName(request));
            request.setAttribute(RapidThemeHandler.RAPID_THEME_HANDLER_KEY, themeHandler);
        }
        return themeHandler;
    }

    protected RapidLanguageHandler getLanguageHandler(HttpServletRequest request) {
        RapidLanguageHandler languageHandler = (RapidLanguageHandler) request.getAttribute(RapidLanguageHandler.RAPID_LANGUAGE_HANDLER_KEY);
        if (languageHandler == null) {
            languageHandler = new RapidLanguageHandler(getLocale(request), getApplicationContext());
            request.setAttribute(RapidLanguageHandler.RAPID_LANGUAGE_HANDLER_KEY, languageHandler);
        }
        return languageHandler;
    }

    protected Locale getLocale(HttpServletRequest request) {
        return RequestContextUtils.getLocale(request);
    }

    @Override
    protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request,
                                            HttpServletResponse response) {

        this.getLanguageHandler(request);
        this.getThemeHandler(request);
        return super.buildTemplateModel(model, request, response);
    }
}
